package com.hf.lookatme.utils;

import android.content.Context;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import java.lang.reflect.Field;

/**
 * Created by JoeShiguang on 2015/5/24.
 */
public class DisplayUtils {


  /**
   * Inflater一个View
   *
   * @param context
   * @param resId
   *
   * @return
   */
  public static View getView(Context context, int resId) {
    LayoutInflater layoutInflater = getInflater(context);
    if (layoutInflater != null) {
      return layoutInflater.inflate(resId, null);
    }
    return null;
  }

  /**
   * 获取LayoutInflater
   *
   * @param context
   *
   * @return
   */
  public static LayoutInflater getInflater(Context context) {
    if (null != context) {
      return (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    return null;
  }

  /**
   * 获取整个屏幕的宽高
   *
   * @param context
   *
   * @return
   */
  public static Point getDefaultDisplayWithAndHeight(Context context) {

    Point point = new Point();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getSize(point);

    return point;
  }

  /**
   * 获取屏幕宽高比
   *
   * @param context
   *
   * @return
   */
  public static float getScreenRate(Context context) {
    Point P = getDefaultDisplayWithAndHeight(context);
    float H = P.y;
    float W = P.x;
    return (H / W);
  }

  /**
   * 获取statusBar的高度
   *
   * @param context
   *
   * @return
   */
  public static int getStatusBarHeight(Context context) {
    try {
      Class<?> c = Class.forName("com.android.internal.R$dimen");
      Object obj = c.newInstance();
      Field field = c.getField("status_bar_height");
      int x = Integer.parseInt(field.get(obj).toString());
      return context.getResources().getDimensionPixelSize(x);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return 0;
  }

}
